<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="160px" :rules="rules">
      <el-row style="text-align: center">
        <el-col :span="12">
          <el-form-item label="整车物料号:" prop="materialNo">
            <el-input v-model="formModel.materialNo" disabled>
              <el-button slot="append" type="primary" icon="el-icon-search" :disabled="disabledMaterialNo" @click="queryMaterialData"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车型:" prop="productTypeKey">
            <hey-select v-model="formModel.productTypeKey" disabled clcd="key_product_type" />
          </el-form-item>
        </el-col>
        <el-col :span="12" >
          <el-form-item label="工程整车号:" prop="vehicleNo">
            <el-input v-model="formModel.vehicleNo" :disabled="disabledVehicleNo" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="工程整车描述:" prop="vehicleDesc">
            <el-input v-model="formModel.vehicleDesc" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="submit-area">
      <el-col :span="24">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </el-col>
    </el-row>
    <el-dialog
      v-dialogDrag
      v-if="showMaterialData"
      :close-on-click-modal="false"
      title="物料主数据"
      :visible.sync="showMaterialData"
      width="90%"
      append-to-body
    >
      <material-data-dialog :edit-data="editData" @queryMaterialDataSuc="queryMaterialDataSuc" />
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: 'MaterialVehicleMnt',
  components: {
    MaterialDataDialog: () => import('@/views/dialog/material-data-dialog'),
  },
  props: ['pageType', 'editData'],
  data() {
    return {
      showMaterialData:false,
      disabledMaterialNo:false,
      disabledVehicleNo:false,
      formModel: {
        rcrdId: '',
        materialNo: '',
        productTypeKey: '',
        vehicleNo: '',
        vehicleDesc: '',
      },
      rules: {
      }
    }
  },
  computed: {

  },
  mounted() {
    this.init()
  },
  methods: {
    queryMaterialData(){
      this.editData.materialTypeKey = 'car'
      this.showMaterialData = true
    },
    queryMaterialDataSuc(data){
      this.formModel.materialNo = data.materialNo
      this.formModel.productTypeKey = data.productTypeKey
    },
    init() {
      this.$form = this.$refs.form
      const { pageType, editData, formModel } = this
      if (pageType !== 'add') {
        this.assignData(editData, formModel)
        this.disabledMaterialNo = true
        this.disabledVehicleNo = true
      }
      this.initRules()
    },
    initRules() {
      const REQUIRED_MSG = this.CONST.REQUIRED_MSG
      this.rules = {
        materialNo: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        vehicleNo: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        vehicleDesc: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
      }
      this.$nextTick(_ => {
        this.$form.clearValidate()
      })
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const { pageType, formModel } = this
          const param = { ...formModel }
          const operateType = pageType === 'edit' ? '02' : '01'
          param.operateType = operateType
          const url = '/trBasMaterialVehicle/maintainTrBasMaterialVehicle'
          const tip = pageType === 'edit' ? '修改成功！' : '新增成功！'
          this.post(url, param).then(res => {
            this.$info(tip, () => {
              this.$emit('mntSuc', res)
              this.closeDialog()
            })
          })
        }
      })
    },
    closeDialog() {
      this.$parent.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
